package com.njtoyo.taxi.admin.rest.controller.business.device;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PassToken;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.mapper.master.entity.ElderlyDeviceManage;
import com.njtoyo.taxi.admin.rest.pojo.dto.ElderlyDeviceAddDto;
import com.njtoyo.taxi.admin.rest.pojo.dto.ElderlyDeviceFunctionUpdateDto;
import com.njtoyo.taxi.admin.rest.presenter.ElderlyDeviceCallCardNumPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.ElderlyDeviceManagePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.device.ElderlyDeviceExportWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.device.ElderlyDeviceQueryWrapper;
import com.njtoyo.taxi.admin.service.business.impl.ElderlyDeviceServiceImpl;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


@RestController
@Api(value = "设备管理", tags = "设备管理")
@RequestMapping("business/device")
public class DeviceController {

    @Resource
    private ElderlyDeviceServiceImpl elderlyDeviceServiceImpl;

    @PostMapping("/addElderlyDevice")
    @ApiOperation(value = "助老设备新增", notes = "权限：elderly_device_add")
    @PermissionScope(names = {"elderly_device_add"})
    RestResult<Boolean> addNew(@ModelAttribute("admin") AdminUser adminUser, @RequestBody ElderlyDeviceAddDto dto) {
        return elderlyDeviceServiceImpl.addNew(adminUser,dto);
    }

    @GetMapping("/getElderlyByDevice")
    @ApiOperation(value = "根据序列号获取设备信息(设备首次启用过程用)")
    @PassToken
    RestResult<ElderlyDeviceManage> getElderlyByDevice(@RequestParam String deviceSerialNumber) {
        RestResult<ElderlyDeviceManage> res = elderlyDeviceServiceImpl.getElderlyDeviceByDeviceSerialNumber(deviceSerialNumber);
        return res;
    }

    @PostMapping("/enableElderlyByDevice")
    @ApiOperation(value = "启用设备-设备端启用")
    @PassToken
    RestResult<Boolean> enableByDevice(@RequestBody ElderlyDeviceAddDto dto) {
        return elderlyDeviceServiceImpl.enableElderlyByDevice(dto);
    }

    @PostMapping("/enableElderlyFromBackend")
    @ApiOperation(value = "启用/禁用设备-平台")
    @PermissionScope(names = {"elderly_device_state"})
    RestResult<Boolean> enableElderlyFromBackend(@RequestBody ElderlyDeviceAddDto dto) {
        return elderlyDeviceServiceImpl.enableElderlyByBackend(dto);
    }

    @GetMapping("/getElderly")
    @ApiOperation(value = "根据序列号获取设备信息（设备详情）-平台")
    @PermissionScope(names = {"elderly_device_detail"})
    RestResult<ElderlyDeviceManage> getElderlyInfo(@RequestParam String deviceSerialNumber) {
        return elderlyDeviceServiceImpl.getInfo(deviceSerialNumber);
    }

    @PostMapping("/editElderlyDevice")
    @ApiOperation(value = "编辑设备")
    @PermissionScope(names = {"elderly_device_edit"})
    RestResult<Boolean> editElderlyDevice(@ModelAttribute("admin") AdminUser adminUser, @RequestBody ElderlyDeviceAddDto dto) {
        return elderlyDeviceServiceImpl.editElderlyDevice(adminUser,dto);
    }

    @PostMapping("/editFunctionDeviceState")
    @ApiOperation(value = "根据设备回传更新功能状态-设备")
    @PassToken
    RestResult<Boolean> editFunctionDeviceState(@RequestBody ElderlyDeviceFunctionUpdateDto dto) {
        return elderlyDeviceServiceImpl.updateElderlyDeviceFunction(dto);
    }

    @GetMapping("/getList")
    @ApiOperation(value = "查询列表")
    @PermissionScope(names = {"elderly_device_list"})
    RestResult<Page<ElderlyDeviceManagePresenter>> getList(@ModelAttribute("admin") AdminUser adminUser, @Valid ElderlyDeviceQueryWrapper wrapper){
        return elderlyDeviceServiceImpl.getList(adminUser,wrapper);
    }

    @GetMapping("/getListByCantonId")
    @ApiOperation(value = "查询所有设备")
    RestResult<List<ElderlyDeviceManagePresenter>> getListByCantonId(@ModelAttribute("admin") AdminUser adminUser, @RequestParam Integer cantonId){
        return elderlyDeviceServiceImpl.getListByCantonId(cantonId);
    }

    @GetMapping("/getCallNumByCard")
    @ApiOperation(value = "获取单个设备卡类型单量")
    RestResult<List<ElderlyDeviceCallCardNumPresenter>> getCallNumByCard(@RequestParam String deviceSerialNumber) {
        return elderlyDeviceServiceImpl.getCallCardNum(deviceSerialNumber);
    }

    @GetMapping("/exportElderlyDevice")
    @ApiOperation(value = "导出")
    @PermissionScope(names = {"elderly_device_export"})
    void  exportElderlyDevice(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                              @Valid ElderlyDeviceExportWrapper wrapper,
                              HttpServletResponse response) throws IOException {
        elderlyDeviceServiceImpl.exportElderlyDeviceList(adminUser,wrapper,response);
    }

    @GetMapping("/getCallNumByAllCard")
    @ApiOperation(value = "获取所有卡类型单量")
    RestResult<ElderlyDeviceCallCardNumPresenter> getCallNumByAllCard() {
        return elderlyDeviceServiceImpl.getAllCallCardNumPresenter();
    }
}
